package com.ebelter.seneo.ui.fragments.main;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ebelter.btcomlib.CommonLib;
import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.common.IOrder;
import com.ebelter.btcomlib.models.db.products.seneo.SeneoUserinfos;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.seneo.R;
import com.ebelter.seneo.beans.httpresponse.OriginalParametersResponse;
import com.ebelter.seneo.contants.IUserContant;
import com.ebelter.seneo.models.SeneoBTManager;
import com.ebelter.seneo.models.SeneoNet;
import com.ebelter.seneo.ui.activitys.SeneoMainActivity;
import com.ebelter.seneo.ui.fragments.BaseFragment;
import com.ebelter.seneo.ui.views.NumSelectorView;
import com.ebelter.seneo.utils.SeneoUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import static com.ebelter.seneo.utils.SeneoUtils.getIdeaWeightMax;
import static com.ebelter.seneo.utils.SeneoUtils.getIdeaWeightMin;
import static com.ebelter.seneo.utils.SeneoUtils.sendCommonEventBus;


//填写密码
public class Goal_Fragment extends BaseFragment {


    public static final String TAG = "Goal_Fragment";

    
    TextView goal_currentWeightValue_tv;
    TextView goal_currentWeightUnit_tv;
    TextView goal_tip1_tv;
    TextView goal_targetWeightValue_tv;
    TextView goal_targetWeightUnit_tv;
    TextView goal_tip2_tv;
    NumSelectorView goal_numselector;
    Button goal_set_bt;

    @Override
    protected int setLayoutResouceId() {
        return R.layout.m_goal_fly;
    }

    @Override
    protected void initView() {

        FV();
    }

    @Override
    protected void initData(Bundle arguments) {
        super.initData(arguments);
        EventBus.getDefault().register(this);
        updateUI();


    }

    private SeneoUserinfos currentUserinfos;
    public void updateUI() {

        if (getActivity()==null) return;

        LogUtils.i2(TAG,TAG+"------updateUI()");
        SeneoBTManager seneoBTManager = ((SeneoMainActivity)getActivity()).getSeneoBTManager();
        if (seneoBTManager!=null){
            currentUserinfos = seneoBTManager.getCurrentUserinfos();
            if (currentUserinfos==null){
                LogUtils.i(TAG,"---updateSexNick-找不到用户信息");
            }else {
                display(currentUserinfos);
            }
        }
    }

    private void display(SeneoUserinfos currentUserinfos) {

        int unit = SeneoUtils.getUnitModel();//0代表kg 1代表lb

        float minIdea = getIdeaWeightMin(currentUserinfos.height);
        float maxIdea = getIdeaWeightMax(currentUserinfos.height);

        //1 设置当前体重的值
        if (currentUserinfos.currentWeight==0){
            ViewUtils.setTextViewStr(goal_currentWeightValue_tv, IUserContant.IDIS.Non);
        }else {
            SeneoUtils.setDisplayValue(goal_currentWeightValue_tv,currentUserinfos.currentWeight,unit);
        }


        if (currentUserinfos.targetWeight==0){
            //说明没有目标值 就按照理想体重的值居中就可以
            currentUserinfos.targetWeight = (float) ((minIdea+maxIdea)*1.0/2);
            currentUserinfos.save();
        }

        goal_numselector_display_value=currentUserinfos.targetWeight;

        float target = SeneoUtils.setDisplayValue(goal_targetWeightValue_tv,currentUserinfos.targetWeight,unit);
        //2
        float minNumselector;
        float maxNumselector;
        if (unit==0){
            ViewUtils.setTextViewStr(goal_currentWeightUnit_tv, IUserContant.IDIS.kg);
            ViewUtils.setTextViewStr(goal_targetWeightUnit_tv, IUserContant.IDIS.kg);
            minNumselector= IUserContant.IDIS.unitkg_min;
            maxNumselector=IUserContant.IDIS.unitkg_max;
        }else {
            ViewUtils.setTextViewStr(goal_currentWeightUnit_tv, IUserContant.IDIS.lb);
            ViewUtils.setTextViewStr(goal_targetWeightUnit_tv, IUserContant.IDIS.lb);
            minNumselector=IUserContant.IDIS.unitlb_min;
            maxNumselector=IUserContant.IDIS.unitlb_max;
        }


        //3

        String ideaWeight= CommonLib.APPContext.getString(R.string.Your_ideal_weight_1_1);

        if (unit==0){
            minIdea = NumUtils.numBaoLiuWei1(minIdea);
            maxIdea = NumUtils.numBaoLiuWei1(maxIdea);
            ViewUtils.setTextViewStr(goal_tip1_tv,String.format(ideaWeight,minIdea+IUserContant.IDIS.kg,maxIdea+IUserContant.IDIS.kg));
        }else {
            //lb
            minIdea=SeneoUtils.kg2lb(minIdea);
            maxIdea=SeneoUtils.kg2lb(maxIdea);
            minIdea = NumUtils.numBaoLiuWei1(minIdea);
            maxIdea = NumUtils.numBaoLiuWei1(maxIdea);
            ViewUtils.setTextViewStr(goal_tip1_tv,String.format(ideaWeight,minIdea+IUserContant.IDIS.lb,maxIdea+IUserContant.IDIS.lb));
        }

        if (goal_numselector!=null){
            goal_numselector.setMinMaxNum(minNumselector,maxNumselector);
            goal_numselector.setValue(target);
        }


        float currentWeightKg = NumUtils.numBaoLiuWei1(currentUserinfos.currentWeight);
        float targetWeightKg = NumUtils.numBaoLiuWei1(currentUserinfos.targetWeight);

        float currentWeightLb = NumUtils.numBaoLiuWei1(SeneoUtils.kg2lb(currentWeightKg));
        float targetWeightLb = NumUtils.numBaoLiuWei1(SeneoUtils.kg2lb(targetWeightKg));



        float chazhi;
        String unitStr;
        if (unit==0){
            chazhi= Math.abs(targetWeightKg-currentWeightKg);
            unitStr=IUserContant.IDIS.kg;
        }else {
            chazhi= Math.abs(targetWeightLb-currentWeightLb);
            unitStr=IUserContant.IDIS.lb;
        }

        //如果当前体重没有的时候就把 下面的提示语隐藏起来的意思
        if (currentWeightKg==0){
            ViewUtils.hideView(goal_tip2_tv);
        }else {
            ViewUtils.displayView(goal_tip2_tv);
        }

        if (currentWeightKg==targetWeightKg){
            ViewUtils.setTextViewStr(goal_tip2_tv,"You are in good shape, keep it up!");
        }else if (currentWeightKg>targetWeightKg){
            ViewUtils.setTextViewStr(goal_tip2_tv,String.format(StringUtils.getResStr(R.string.Lose_1_to_achieve_this_goal),chazhi,unitStr));
        }else {
            ViewUtils.setTextViewStr(goal_tip2_tv,String.format(StringUtils.getResStr(R.string.Gain_1_1f_to_achieve_this_goal),chazhi,unitStr));
        }

    }




    private void FV() {
        goal_currentWeightValue_tv=findViewById(R.id.goal_currentWeightValue_tv);
        goal_currentWeightUnit_tv=findViewById(R.id.goal_currentWeightUnit_tv);
        goal_tip1_tv=findViewById(R.id.goal_tip1_tv);
        goal_targetWeightValue_tv=findViewById(R.id.goal_targetWeightValue_tv);
        goal_targetWeightUnit_tv=findViewById(R.id.goal_targetWeightUnit_tv);
        goal_tip2_tv=findViewById(R.id.goal_tip2_tv);
        goal_numselector=findViewById(R.id.goal_numselector);
        goal_set_bt=findViewById(R.id.goal_set_bt);
    }


    private float goal_numselector_display_value;//永远是kg
    @Override
    protected void setListener() {
        goal_numselector.setmValueChangeListener(new NumSelectorView.ValueChangeListener() {
            @Override
            public void changeValue(float value) {
                float v=NumUtils.numBaoLiuWei1(value);
                ViewUtils.setTextViewStr(goal_targetWeightValue_tv, v+"");
             //   goal_numselector_display_value=v;
//                if (currentUserinfos!=null){
                    if (SeneoUtils.getUnitModel()==0){
                        goal_numselector_display_value=v;
                    }else {
                        goal_numselector_display_value=NumUtils.numBaoLiuWei1(SeneoUtils.lb2kg(v));
                    }
//                    currentUserinfos.save();
//                    display(currentUserinfos);
//                }


            }
        });
        goal_set_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentUserinfos==null){
                    return;
                }
                Map<String,Serializable> paramMap=new HashMap<>();
                paramMap.put("userId", currentUserinfos.user_id);
                paramMap.put("targetWeight",goal_numselector_display_value);

                SeneoNet.getIntance().setTargetWeight(Goal_Fragment.TAG, paramMap, new HttpResponse<OriginalParametersResponse>() {
                    @Override
                    public void result(boolean isRequestSuccess, String requestDes, OriginalParametersResponse originalParametersResponse, String s) {
                        if (isRequestSuccess){
                            if (originalParametersResponse.resultCode==0){
                                ToastUtil.show(R.string.Set_success);


                                currentUserinfos.targetWeight=goal_numselector_display_value;
                                currentUserinfos.save();

                                sendCommonEventBus(TAG,Trend_Fragment.TAG,IUserContant.ICMDS.UPDATE_TARGET_WEIGHT,"更新目标体重");


                            }else if (originalParametersResponse.resultCode==110){
                                ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                            } else {
                                ToastUtil.show(R.string.Modify_Failed);
                            }
                        }else {
                            ToastUtil.show(R.string.Network_connection_error);
                        }
                    }
                });



            }
        });
    }





    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(CommonEventBus event) {
        if (!event.whichReceive.contains(TAG)) {
            return;
        }
        LogUtils.i(TAG, TAG+"--onEventMainThread接收到CommonEventBus信息--" + event.toString());
        if (event.cmd== IUserContant.ICMDS.UPDATE_CURRENT_USER_INFO){
            LogUtils.i(TAG, TAG+"--onEventMainThread更新当前用户信息");
            updateUI();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
